----一些公共方法
--#include "data\config\common\CommonTeleportConfig.lua" once			--通用传送
--#include "data\functions\Common\CommonFunc.lua" once					--一些通用方法

--[[
通用传送
]]
function CommonTeleportByTeleTypeAndId( sysarg, teleType, teleId )
	--print("CommonTeleportByTeleTypeAndId, teleType="..teleType..", teleId="..teleId)
	local teleCfg = GetCommonTeleCfgByTeleTypeAndId( teleType, teleId )
	if teleCfg then
		if CommonTeleportByTeleCfg( sysarg, teleCfg ) then
			if teleType == 1 then
				Actor.closeDialogs( sysarg, enLinkActivityGuideWnd )			--关闭活动引导面板
				Actor.closeDialogs( sysarg, enLinkActivityGuideWnd2 )			--关闭活动引导面板的弹出面板
			elseif teleType == 2 then
				Actor.closeDialogs( sysarg, enLinkBossWnd )						--关闭激战BOSS面板
			end
		end
	end
end

function CommonTeleportByTeleId( sysarg, teleId )
	--print("CommonTeleportByTeleId, teleId="..teleId)
	local teleCfg = GetCommonTeleCfgByTeleId( teleId )
	if teleCfg then
		return CommonTeleportByTeleCfg( sysarg, teleCfg )
	end
end

function CommonTeleportByTeleCfg( sysarg, teleCfg )
	if not teleCfg then
		return false
	end

	if Actor.getIsDartFlag(sysarg) then
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.EscortTips024,ttFlyTip)
        return false 
	end

	if teleCfg.levelLimit and not Actor.checkActorLevel( sysarg, teleCfg.levelLimit[2], teleCfg.levelLimit[1] ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0016,ttFlyTip)
		return false
	end

	if teleCfg.vipLimit and teleCfg.vipLimit > 0 and Actor.getVipGrade( sysarg ) < teleCfg.vipLimit then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.COM0050, teleCfg.vipLimit),ttFlyTip)
		return false
	end 

	if teleCfg.validTimes and not CheckTimesValid( teleCfg.validTimes ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0036,ttFlyTip)
		return false
	end

	if not (teleCfg.canCross and teleCfg.canCross == 1 ) and Actor.IsCross( sysarg ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0084,ttFlyTip)
		return false
	end

	if teleCfg.notCommon and teleCfg.notCommon == 1 and not Actor.IsCross( sysarg ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0056,ttFlyTip)
		return false
	end

	local sceneId 	= teleCfg.sceneId or 0
	local telePos 	= teleCfg.telePos
	local npcId		= teleCfg.openNpc or 0
	local winId		= 0
	local tabId		= 0

	--vipTel={ needVip=3, sceneId=9, pos={xx,xx}, }
	if teleCfg.vipTel then  --VIP可以速传
		if Actor.getVipGrade( sysarg ) >= teleCfg.vipTel.needVip then
			sceneId = teleCfg.vipTel.sceneId
			telePos = teleCfg.vipTel.pos
		end
	end 
	
	if teleCfg.openWin then
		winId = teleCfg.openWin[1] or 0
		tabId = teleCfg.openWin[2] or 0
	end

	local nowSceneId = Actor.getSceneId( sysarg )
	if IsElemInList( CommonTeleportScenesReject, nowSceneId ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0056,ttFlyTip)
		return false
	end

	if sceneId and sceneId > 0 and telePos then
		if not Actor.canTelport(sysarg, sceneId ) then               --是否允许场景传送
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0020,ttFlyTip)
			return false
		end

		if Actor.enterScene(sysarg, sceneId, telePos[1], telePos[2]) then
			Actor.sendCommonTeleportResult( sysarg, teleCfg.teleId )			--传送成功，通知客户端，其他操作，由NPC完成
			--[[
			if npcId and npcId > 0 then			--传送成功，打开NPC
				local npcPtr = Fuben.getNpcPtrById(sceneId, npcId)
				if npcPtr then
					Actor.OnNpcTalk(sysarg, npcPtr, "");
				end
			end
			]]
		end
	end

	--print("CommonTeleportByTeleCfg, winId="..winId)
	--[[
	if winId and winId > 0 then				--打开对话框
		Actor.openDialogs( sysarg, winId, tostring(tabId) )
	end
	]]
	Actor.closeDialogs( sysarg, enLinkActivityGuideWnd )			--关闭活动引导面板
	Actor.closeDialogs( sysarg, enLinkActivityGuideWnd2 )			--关闭活动引导面板的弹出面板
	return true
end


function GetCommonTeleCfgByTeleTypeAndId( teleType, teleId )
	for teleType, teleCfg in ipairs( CommonTeleportCfg[teleType] or {} ) do
		if teleCfg.teleId == teleId then
			return teleCfg
		end
	end
	return nil
end

function GetCommonTeleCfgByTeleId( teleId )
	for teleType, teleTypeCfg in ipairs( CommonTeleportCfg ) do
		for teleIdx, teleCfg in ipairs( teleTypeCfg ) do
			if teleCfg.teleId == teleId then
				return teleCfg
			end
		end
	end
	return nil
end
